package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.j2objc.annotations.AutoreleasePool;
import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.exception.BaseException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageBean;
import com.sky.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.parser.BaseToken;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 分类业务层
 */
@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 分类分页查询
     *
     * @param dto dto
     * @return pagebean
     */
    @Override
    public PageBean pageQuery(CategoryPageQueryDTO dto) {
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        Page<Category> categories = categoryMapper.pageQuery(dto);
        return new PageBean(categories.getTotal(), categories.getResult());
    }

    /**
     * 新增分类
     *
     * @param categoryDTO dto
     */
    @Override
    public void add(CategoryDTO categoryDTO) {
        Category category = Category.builder()
                .createTime(LocalDateTime.now())
                .updateTime(LocalDateTime.now())
                .createUser(BaseContext.getCurrentId())
                .updateUser(BaseContext.getCurrentId()).build();
        BeanUtils.copyProperties(categoryDTO, category);
        categoryMapper.add(category);
    }

    /**
     * 根据id删除分类
     *
     * @param id 分类id
     */
    @Override
    public void deleteById(Long id) {
        //- 如果当前分类下关联了菜品，直接返回错误提示信息（抛出异常）
        //- 步骤2：如果当前分类下关联了套餐，直接返回错误提示信息（抛出异常）
        //- 步骤3：删除分类数据
        List<Dish> dishes = dishMapper.selectByCategoryId(id);
        if (dishes.size() > 0) {
            throw new BaseException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }

        List<Setmeal> setmeals = setmealMapper.selectByCategoryId(id);
        if (setmeals.size() > 0) {
            throw new BaseException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        categoryMapper.deleteById(id);
    }

    /**
     * 修改分类
     *
     * @param categoryDTO dto
     */
    @Override
    public void update(CategoryDTO categoryDTO) {
        Category category = Category.builder()
                .updateTime(LocalDateTime.now())
                .updateUser(BaseContext.getCurrentId())
                .build();
        BeanUtils.copyProperties(categoryDTO, category);
        categoryMapper.update(category);
    }

    /**
     * 启用、禁用分类
     *
     * @param status 状态
     * @param id     分类id
     */
    @Override
    public void updateStatus(Integer status, Long id) {
        Category category = Category.builder()
                .status(status)
                .id(id)
                .updateTime(LocalDateTime.now())
                .updateUser(BaseContext.getCurrentId())
                .build();

        categoryMapper.update(category);
    }

    /**
     * 根据类型查询分类,查询出状态为启用的分类，并按照指定字段排序
     *
     * @param type 分类类型：1为菜品分类，2为套餐分类
     * @return 分类集合
     */
    @Override
    public List<Category> selectByType(Integer type) {
        List<Category> categories = categoryMapper.selectByType(type);
        return categories;
    }
}
